package cn.com.zero.sample.user.controller;


import cn.com.zero.api.log.annotation.Log;
import cn.com.zero.sample.user.service.SessionService;
import cn.com.zero.sample.user.service.UserService;
import cn.com.zero.sample.user.vo.UserInfo;
import cn.com.zero.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;


/**
 * @author zhoupeng
 * @version 1.0.0
 * @Description 用户接口
 * @createTime 2022-07-11
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    private final static String phone_ex = "^(1[0-9]{10})$";

    private UserService userService;

    private SessionService sessionService;

//    private ZeroJobService zeroJobService;

    public UserController(UserService userService, SessionService sessionService) {
//    public UserController(UserService userService, SessionService sessionService, ZeroJobService zeroJobService) {
        this.userService = userService;
        this.sessionService = sessionService;
//        this.zeroJobService = zeroJobService;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名"),
            @ApiImplicitParam(name = "password", value = "密码"),
            @ApiImplicitParam(name = "nick", value = "昵称")
    })
    @ApiOperation(value = "用户注册")
    @GetMapping("/register")
    @Log(logType = 1, content = "使用用户名注册用户")
    public UserInfo register(@NotBlank(message = "-10001") @Length(min = 8, max = 16, message = "-10002") @RequestParam(required = false) String username,
                             @NotBlank(message = "-10003") @Length(min = 8, max = 32, message = "-10004") @RequestParam(required = false) String password,
                             @NotBlank(message = "-10005") @Length(min = 2, max = 12, message = "-10006") @RequestParam(required = false) String nick) {
        logger.info("{}新用户开始注册", username);
        return userService.register(username, password, nick);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名"),
            @ApiImplicitParam(name = "password", value = "密码")
    })
    @ApiOperation(value = "用户登录")
    @GetMapping("/login")
    @Log(logType = 2, content = "用户名登录")
    public UserInfo login(@NotBlank(message = "-10001") @RequestParam(required = false) String username,
                          @NotBlank(message = "-10003") @RequestParam(required = false) String password,
                          HttpServletResponse response) {
        UserInfo userInfo = userService.login(username, password);
        String session = sessionService.createSession(userInfo.getUsername());
        response.addCookie(new Cookie("session", session));
        return userInfo;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号")
    })
    @ApiOperation(value = "发送用户注册验证码")
    @PostMapping("/code/register")
    public void sendRegisterCode(@NotBlank(message = "-10009") @Pattern(regexp = phone_ex, message = "-10010") @RequestParam(value = "phone", required = false) String phone) {
        userService.sendRegisterCode(phone);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号"),
            @ApiImplicitParam(name = "verificationCode", value = "注册验证码")
    })
    @ApiOperation(value = "使用手机号注册")
    @PostMapping("/register/phone")
    @Log(logType = 1, content = "使用手机号注册用户")
    public String registerByPhone(@NotBlank(message = "-10009") @Pattern(regexp = phone_ex, message = "-10010") @RequestParam(value = "phone", required = false) String phone,
                                  @NotBlank(message = "-10011") @RequestParam(value = "verificationCode", required = false) String verificationCode) {
        return userService.registerByPhone(phone, verificationCode);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号")
    })
    @ApiOperation(value = "发送用户登录验证码")
    @PostMapping("/code/login")
    public void sendLoginCode(@NotBlank(message = "-10009") @Pattern(regexp = phone_ex, message = "-10010") @RequestParam(value = "phone", required = false) String phone) {
        userService.sendLoginCode(phone);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号"),
            @ApiImplicitParam(name = "verificationCode", value = "登录验证码")
    })
    @ApiOperation(value = "使用手机号登录")
    @PostMapping("/login/phone")
    @Log(logType = 2, content = "手机号登录")
    public String loginByPhone(@NotBlank(message = "-10001") @Pattern(regexp = phone_ex, message = "-10010") @RequestParam(value = "phone", required = false) String phone,
                               @NotBlank(message = "-10011") @RequestParam(value = "verificationCode", required = false) String verificationCode) {
        return userService.loginByPhone(phone, verificationCode);
    }


//    @PostMapping("/start-check")
//    public void startCheckSession() {
//        zeroJobService.startJob(CheckSessionJob.NAME);
//    }
//
//    @PostMapping("/stop-check")
//    public void stopCheckSession() {
//        zeroJobService.stopJob(CheckSessionJob.NAME);
//    }
}